home *** CD-ROM | disk | FTP | other *** search
- Imports System.Data
- Imports System.Data.OleDb
- Imports System.Data.SqlClient
- Imports System.IO
-
- Public Class XmlDataSetForm
- Inherits System.Windows.Forms.Form
-
- #Region " Windows Form Designer generated code "
-
- Public Sub New()
- MyBase.New()
-
- 'This call is required by the Windows Form Designer.
- InitializeComponent()
-
- 'Add any initialization after the InitializeComponent() call
-
- End Sub
-
- 'Form overrides dispose to clean up the component list.
- Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
- If disposing Then
- If Not (components Is Nothing) Then
- components.Dispose()
- End If
- End If
- MyBase.Dispose(disposing)
- End Sub
-
- 'Required by the Windows Form Designer
- Private components As System.ComponentModel.IContainer
-
- 'NOTE: The following procedure is required by the Windows Form Designer
- 'It can be modified using the Windows Form Designer.
- 'Do not modify it using the code editor.
- Friend WithEvents txtOut As System.Windows.Forms.TextBox
- Friend WithEvents btnWriteXml As System.Windows.Forms.Button
- Friend WithEvents Button1 As System.Windows.Forms.Button
- Friend WithEvents btnWriteDataSchema As System.Windows.Forms.Button
- Friend WithEvents btnWriteSchema As System.Windows.Forms.Button
- Friend WithEvents btnDiffGram As System.Windows.Forms.Button
- Friend WithEvents btnGetChanges As System.Windows.Forms.Button
- Friend WithEvents btnStronglyTyped As System.Windows.Forms.Button
- <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
- Me.btnWriteXml = New System.Windows.Forms.Button()
- Me.txtOut = New System.Windows.Forms.TextBox()
- Me.Button1 = New System.Windows.Forms.Button()
- Me.btnWriteDataSchema = New System.Windows.Forms.Button()
- Me.btnWriteSchema = New System.Windows.Forms.Button()
- Me.btnDiffGram = New System.Windows.Forms.Button()
- Me.btnGetChanges = New System.Windows.Forms.Button()
- Me.btnStronglyTyped = New System.Windows.Forms.Button()
- Me.SuspendLayout()
- '
- 'btnWriteXml
- '
- Me.btnWriteXml.Location = New System.Drawing.Point(8, 16)
- Me.btnWriteXml.Name = "btnWriteXml"
- Me.btnWriteXml.Size = New System.Drawing.Size(104, 40)
- Me.btnWriteXml.TabIndex = 0
- Me.btnWriteXml.Text = "WriteXml"
- '
- 'txtOut
- '
- Me.txtOut.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
- Or System.Windows.Forms.AnchorStyles.Left) _
- Or System.Windows.Forms.AnchorStyles.Right)
- Me.txtOut.Location = New System.Drawing.Point(120, 16)
- Me.txtOut.Multiline = True
- Me.txtOut.Name = "txtOut"
- Me.txtOut.ScrollBars = System.Windows.Forms.ScrollBars.Both
- Me.txtOut.Size = New System.Drawing.Size(520, 328)
- Me.txtOut.TabIndex = 1
- Me.txtOut.Text = ""
- Me.txtOut.WordWrap = False
- '
- 'Button1
- '
- Me.Button1.Location = New System.Drawing.Point(8, 64)
- Me.Button1.Name = "Button1"
- Me.Button1.Size = New System.Drawing.Size(104, 40)
- Me.Button1.TabIndex = 2
- Me.Button1.Text = "WriteXml (nested)"
- '
- 'btnWriteDataSchema
- '
- Me.btnWriteDataSchema.Location = New System.Drawing.Point(8, 112)
- Me.btnWriteDataSchema.Name = "btnWriteDataSchema"
- Me.btnWriteDataSchema.Size = New System.Drawing.Size(104, 40)
- Me.btnWriteDataSchema.TabIndex = 3
- Me.btnWriteDataSchema.Text = "Write data and Schema"
- '
- 'btnWriteSchema
- '
- Me.btnWriteSchema.Location = New System.Drawing.Point(8, 160)
- Me.btnWriteSchema.Name = "btnWriteSchema"
- Me.btnWriteSchema.Size = New System.Drawing.Size(104, 40)
- Me.btnWriteSchema.TabIndex = 4
- Me.btnWriteSchema.Text = "Write Schema only"
- '
- 'btnDiffGram
- '
- Me.btnDiffGram.Location = New System.Drawing.Point(8, 208)
- Me.btnDiffGram.Name = "btnDiffGram"
- Me.btnDiffGram.Size = New System.Drawing.Size(104, 40)
- Me.btnDiffGram.TabIndex = 5
- Me.btnDiffGram.Text = "Write DiffGram"
- '
- 'btnGetChanges
- '
- Me.btnGetChanges.Location = New System.Drawing.Point(8, 256)
- Me.btnGetChanges.Name = "btnGetChanges"
- Me.btnGetChanges.Size = New System.Drawing.Size(104, 40)
- Me.btnGetChanges.TabIndex = 6
- Me.btnGetChanges.Text = "GetChanges + DiffGram"
- '
- 'btnStronglyTyped
- '
- Me.btnStronglyTyped.Location = New System.Drawing.Point(8, 304)
- Me.btnStronglyTyped.Name = "btnStronglyTyped"
- Me.btnStronglyTyped.Size = New System.Drawing.Size(104, 40)
- Me.btnStronglyTyped.TabIndex = 7
- Me.btnStronglyTyped.Text = "Strongly-typed DS"
- '
- 'XmlDataSetForm
- '
- Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
- Me.ClientSize = New System.Drawing.Size(656, 357)
- Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnStronglyTyped, Me.btnGetChanges, Me.btnDiffGram, Me.btnWriteSchema, Me.btnWriteDataSchema, Me.Button1, Me.txtOut, Me.btnWriteXml})
- Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
- Me.Name = "XmlDataSetForm"
- Me.Text = "XML and DataSets"
- Me.ResumeLayout(False)
-
- End Sub
-
- #End Region
-
- Dim ds As New DataSet()
-
- ' fill the DataSet from Pubs db
-
- Sub FillDataSet()
- ds = New DataSet()
- ' Fill the data set with data from two tables
- Dim cn As New OleDbConnection(OledbPubsConnString)
- cn.Open()
- Dim daPub As New OleDbDataAdapter("SELECT * FROM Publishers", cn)
- Dim daTit As New OleDbDataAdapter("SELECT * FROM Titles", cn)
- daPub.Fill(ds, "Publishers")
- daTit.Fill(ds, "Titles")
- cn.Close()
- ' Create a relationship between them.
- ds.Relations.Add("PubTitles", ds.Tables("Publishers").Columns("pub_id"), ds.Tables("Titles").Columns("pub_id"))
- End Sub
-
- ' Read the contents of a file.
- Function GetTextFile(ByVal filename As String) As String
- Dim sr As New StreamReader(filename)
- GetTextFile = sr.ReadToEnd
- sr.Close()
- End Function
-
- ' write the dataset to XML
-
- Private Sub btnWriteXml_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteXml.Click
- FillDataSet()
- ds.WriteXml("ds.xml")
- txtOut.Text = GetTextFile("ds.xml")
- txtOut.Text = ds.GetXml
- End Sub
-
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- FillDataSet()
- ds.Relations("PubTitles").Nested = True
- 'ds.WriteXml("ds.xml")
- 'txtOut.Text = GetTextFile("ds.xml")
-
- #Const USE_TABLEMAPPINGS = True
-
- #If USE_TABLEMAPPINGS Then
- ' Set the ColumnMapping property for some columns.
- ds.Tables("Publishers").Columns("pub_id").ColumnMapping = MappingType.Attribute
- ds.Tables("Publishers").Columns("country").ColumnMapping = MappingType.Hidden
- #End If
-
- txtOut.Text = ds.GetXml
- End Sub
-
- ' write the XML schema and the data
-
- Private Sub btnWriteDataSchema_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteDataSchema.Click
- FillDataSet()
- ds.WriteXml("ds.xml", XmlWriteMode.WriteSchema)
- txtOut.Text = GetTextFile("ds.xml")
- End Sub
-
- ' write the XML schema only
-
- Private Sub btnWriteSchema_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteSchema.Click
- FillDataSet()
- ds.WriteXmlSchema("ds.xsd")
- txtOut.Text = GetTextFile("ds.xsd")
- End Sub
-
- ' create a DiffGram
-
- Private Sub btnDiffGram_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDiffGram.Click
- FillDataSet()
- ' Make some changes to the Titles table.
- With ds.Tables("Titles")
- ' Delete the first row.
- .Rows(0).Delete()
- ' Modify two fields in the second row.
- .Rows(1)("price") = 49.99
- .Rows(1)("advance") = 12300
- ' Insert a new row.
- Dim dr As DataRow = .NewRow
- dr("title") = "Programming VB.NET"
- dr("type") = "technical"
- dr("price") = 59.99
- .Rows.Add(dr)
- End With
-
- ds.WriteXml("ds.xml", XmlWriteMode.DiffGram)
- txtOut.Text = GetTextFile("ds.xml")
- End Sub
-
- ' create a DataSet that contain only the changes
-
- Private Sub btnGetChanges_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetChanges.Click
- FillDataSet()
- ' Make some changes to the Titles table.
- With ds.Tables("Titles")
- ' Delete the first row.
- .Rows(0).Delete()
- ' Modify two fields in the second row.
- .Rows(1)("price") = 49.99
- .Rows(1)("advance") = 12300
- ' Insert a new row.
- Dim dr As DataRow = .NewRow
- dr("title") = "Programming VB.NET"
- dr("type") = "technical"
- dr("price") = 59.99
- .Rows.Add(dr)
- End With
- Dim ds2 As DataSet = ds.GetChanges
- ds2.WriteXml("ds.xml", XmlWriteMode.DiffGram)
- txtOut.Text = GetTextFile("ds.xml")
- End Sub
-
- ' use the Strongly-typed DataSet
-
- Private Sub btnStronglyTyped_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStronglyTyped.Click
- Dim pubsDs As New PubsDataSet()
- ' Add a new row to the Publishers table.
- Dim pubsRow As PubsDataSet.PublishersRow = pubsDs.Publishers.NewPublishersRow
- pubsRow.pub_id = "1234"
- pubsRow.pub_name = "VB2TheMax"
- pubsRow.city = "Bari"
- pubsRow.country = "Italy"
- pubsDs.Publishers.AddPublishersRow(pubsRow)
-
- If pubsDs.Publishers(0).IscityNull Then
- pubsDs.Publishers(0).SetcountryNull()
- End If
- End Sub
- End Class
-
-